package com.m3958.firstgwt.client.event;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.event.shared.EventBus;
import com.google.inject.Inject;
import com.m3958.firstgwt.client.IHasSiteChangHandler;

public class SiteChangeListener {
	
	private EventBus eventBus;
	
	private List<IHasSiteChangHandler> listeners;
	
	@Inject
	public SiteChangeListener(EventBus eventBus){
		this.eventBus = eventBus;
		listeners = new ArrayList<IHasSiteChangHandler>();
		initHanlder();
	}
	
	
	private void initHanlder() {
		eventBus.addHandler(SiteChangeEvent.TYPE, new SiteChangeEventHandler() {
			
			@Override
			public void onSiteChange(SiteChangeEvent event) {
				for(IHasSiteChangHandler listener : listeners){
					listener.onSiteChange(event.getSiteId());
				}
			}
		});
	}


	public void addListener(IHasSiteChangHandler isc){
		listeners.add(isc);
	}
	
	public void removeListener(IHasSiteChangHandler isc){
		listeners.remove(isc);
	}
}
